<?php

//Call this file only from server
if ($_SERVER["REMOTE_ADDR"] != $_SERVER["SERVER_ADDR"]) die("Invalid Request"); 
    
	include("../config.php");
	include("../inc/common.php");
    include("../inc/class.database.php");
	include("../inc/db_connect.php");
	
	$result = $db->query( "SELECT COUNT(*) FROM ".OSDB_GAMES." 
	WHERE LOWER(map) LIKE LOWER('%dota%') AND stats = 0 AND duration>='".$MinDuration."'" );
    $r = $db->fetch_row($result);
    $Total = $r[0];
	
	if ( $Total>=1 ) {
	//GET ALL ADMINS
	$result = $db->query("SELECT * FROM ".OSDB_ADMINS." WHERE id>=1");
	$admins = array();
	while ($row = $db->fetch_array($result,'assoc')) { $admins[]= strtolower($row["name"]);  }
	//GET ALL USERS FROM SAFELIST
	$result = $db->query("SELECT * FROM ".OSDB_SAFELIST." WHERE id>=1");
	$safelist = array();
	while ($row = $db->fetch_array($result,'assoc')) { $safelist[]= strtolower($row["name"]);	}
	
	$_result = $db->query( "SELECT id FROM ".OSDB_GAMES." 
	WHERE LOWER(map) LIKE LOWER('%dota%') AND stats = 0 AND duration>='".$MinDuration."' LIMIT ".$updateGamesCron." " );
	
	while ($row = $db->fetch_array($_result,'assoc')) {
	 $gid = $row["id"];
	 $result = $db->query("SELECT winner, dp.gameid, gp.colour, newcolour, kills, deaths, assists, creepkills, creepdenies, neutralkills, towerkills, gold,  raxkills, courierkills, g.duration as duration,
	   gp.name as name, 
	   gp.ip as ip,
	   b.name as banname 
	   FROM ".OSDB_DP." AS dp 
	   LEFT JOIN ".OSDB_DP." AS gp ON gp.gameid = dp.gameid and dp.colour = gp.colour 
	   LEFT JOIN ".OSDB_DG." AS dg ON dg.gameid = dp.gameid 
	   LEFT JOIN ".OSDB_GAMES." AS g ON g.id = dp.gameid 
	   LEFT JOIN ".OSDB_BANS." as b ON b.name=gp.name
	   WHERE dp.gameid='".$gid."'
	   GROUP by gp.name
	   ORDER BY newcolour");
	   
	   if ($db->num_rows($result) <=0)  $update = $db->query("UPDATE ".OSDB_GAMES." SET stats = 1 WHERE id = $gid ;");
	   
	   while ($list = $db->fetch_array($result,'assoc')) {
		$kills=$list["kills"];
		$deaths=$list["deaths"];
		$assists=$list["assists"];
		$creepkills=$list["creepkills"];
		$creepdenies=$list["creepdenies"];
		$neutralkills=$list["neutralkills"];
		$towerkills=$list["towerkills"];
		$raxkills=$list["raxkills"];
		$courierkills=$list["courierkills"];
		$duration=$list["duration"];
		$name=mysql_real_escape_string(trim($list["name"]));
		$IPaddress = $list["ip"];
		$banname=$list["banname"];
		$win=$list["winner"];
		$newcolour=$list["newcolour"];
		
		$warn_expire = $list["expiredate"];
		$warn = $list["warn"];
		
		if ( $warn>=1 ) $warn_qry = 'warn = '.$warn.', '; else $warn_qry = "";
				
		if ( in_array( strtolower($name), $admins ) )   $is_admin = 1; else $is_admin = 0;
		if ( in_array( strtolower($name), $safelist ) ) $is_safe = 1;  else $is_safe  = 0;
		
		if ( strtolower($banname)==strtolower($name) ) $BANNED = 1; else $BANNED = 0;
		
		if ($win==1 AND $newcolour<=5) {$winner = 1; $loser = 0;}
		if ($win==0) {$winner = 0; $loser = 0;}
		if ($win==2 AND $newcolour>5) {$winner = 1; $loser = 0;}
		if ($win==1 AND $newcolour>5) {$winner = 0; $loser = 1;}
		if ($win==2 AND $newcolour<=5) {$winner = 0; $loser = 1;}
		
		if ($winner == 1) $score = $ScoreStart + $ScoreWins;
		if ($winner == 0) $score = $ScoreStart - $ScoreLosses;
		if ($win==0) $score = $ScoreStart;
		
		// Score formula for each game (uncomment below to user score formula)
		// $scoreFormula = (((($kills-$deaths+$assists*0.5+$towerkills*0.5+$raxkills*0.2+($courierkills+$creepdenies)*0.1+$neutralkills*0.03+$creepkills*0.03) * .2)+($score)));
		
		// $score = $scoreFormula;
		
		if ($win==0) $draw = 1; else $draw = 0;
		if (!empty($name) AND $duration >= $MinDuration) {
		$result2 = $db->query("SELECT player FROM ".OSDB_STATS." WHERE LOWER(player) = LOWER('$name')");
		//Create a new player...
		  if ( $db->num_rows($result2) <=0) {
          $sql3 = "INSERT INTO ".OSDB_STATS."(player, score, games, wins, losses, draw, kills, deaths, assists, creeps, denies, neutrals, towers, rax, banned, ip) 
		  VALUES('$name',$score,1,$winner,$loser,$draw,$kills,$deaths,$assists,$creepkills,$creepdenies,$neutralkills, $towerkills, $raxkills, $BANNED, '$IPaddress')";
          } else {
		  //...or update player data
		  if ($winner == 1) $score = "score = score + $ScoreWins,";
		  if ($winner == 0) $score = "score = score - $ScoreLosses,";
		  if ($win==0) $score = "";
		  $sql3 = "UPDATE ".OSDB_STATS." SET 
		  $score
		  player = '$name',
		  games = games+1, 
		  wins = wins +$winner,
		  losses = losses+$loser,
		  draw = draw + $draw,
		  kills = kills + $kills,
		  deaths = deaths + $deaths,
		  assists = assists + $assists,
		  creeps = creeps + $creepkills,
		  denies = denies + $creepdenies,
		  neutrals = neutrals + $neutralkills,
		  towers = towers + $towerkills,
		  rax = rax + $raxkills,
          banned = $BANNED,
		  ip = '$IPaddress'
		  WHERE LOWER(player) = LOWER('$name');";
		   }
		  $result3 = $db->query($sql3);
		 }
		 //$return.="\nGame ($gid) updated!";
	     //Update "games" table so we can know what games have been updated
	     $update = $db->query("UPDATE ".OSDB_GAMES." SET stats = 1 WHERE id = $gid ;");
		 
	   }
	   $return.="\nGame ($gid) updated!";
	}
	
  }
?>